# /usr/bin/python
# -*- coding: utf-8 -*-

import os
import re
import time
import json
import logging
import requests
import commands
logging.basicConfig()
from apscheduler.schedulers.blocking import BlockingScheduler

###
LOGDIR="/data/logs/nginx/access/"

def get_file(logdir):
    file_name = []
    files = os.walk(logdir)

    for file in list(files)[0][2]:
        if re.match(r".+?.log$",file):
            file_name.append(LOGDIR + file)

    return file_name

def get_file_end(logfile):
    f = file(logfile,'r')
    f.seek(0,2)
    file_end = f.tell()
    f.close()
    return file_end

def status_count(logfile,start):
    domain = '.'.join(logfile.split('.')[1:-1])
    statistic = {
        domain:{
            "2XX": 0,
            "3XX": 0,
            "4XX": 0,
            "5XX":0
        }
    }

    filter = r"\"(?P<datetime>.+?)\" \| (?P<domain>.+?) \| (?P<status>.+?) \| (?P<requests_time>.+?) \| (?P<thttp_x_forwarded_for>.+?) \| (?P<body_bytes_sent>.+?) \| (?P<requests>.*)"
    f = file(logfile,'r')
    f.seek(start,0)
    lines = f.readlines()

    for line in lines:
        res = re.match(filter,line).groupdict()
        statistic[domain][res["status"][0]+"XX"] += 1

    f.close()

    return statistic

def get_payload(data):
    payload = []
    for domain in data:
        for item in data[domain]:
            payload.append(get_template(domain,item,data[domain][item],ip))
    
    return payload

def get_template(endpoint,metirc,value,ip):
    template = {
        "endpoint": str(ip),
        "metric": str(metirc),
        "timestamp": int(time.time()),
        "step": 60,
        "value": value,
        "counterType": "GAUGE",
        "tags": "domain=" + str(endpoint),
    }
    return template

def read_logfile(logfile):
    print "read logfile:%s"%logfile
    report = status_count(logfile,end_point[logfile])
    payload = get_payload(report)
    end_point[logfile] = get_file_end(logfile)
    res = requests.post("http://127.0.0.1:1988/v1/push",data=json.dumps(payload))
    print payload
    print res


if __name__ == "__main__":
    files = get_file(LOGDIR)
    ip_cmd = "ip a | grep global | awk '{print $2}' | awk -F '/' '{print $1}'"
    ip = commands.getoutput(ip_cmd)
    global end_point
    end_point = {}
    scheduler = BlockingScheduler()

    for file_name in files:
        print file_name
        end_point[file_name] = get_file_end(file_name)
        scheduler.add_job(read_logfile,args=[file_name],trigger='cron',minute='*')

    try:
        scheduler.start()
    except KeyboardInterrupt:
        scheduler.shutdown()
